﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HitchHikersGuideToTheGalaxy.Models
{
    public class UserRoleRepository : IUserRoleRepository
    {
        private GalaxyDataContext db = new GalaxyDataContext();

        #region Default Add, Delete and save actions

        /// <summary>
        /// Add UserRole
        /// </summary>
        /// <param name="vehicle"></param>
        public void Add(UserRole userRole)
        {
            db.UserRoles.InsertOnSubmit(userRole);
        }

        /// <summary>
        /// Delete UserRole
        /// </summary>
        /// <param name="vehicle"></param>
        public void Delete(UserRole userRole)
        {
            db.UserRoles.DeleteOnSubmit(userRole);
        }

        /// <summary>
        /// Save changes to database
        /// </summary>
        public void Save()
        {
            db.SubmitChanges();
        }

        #endregion

        #region Querys

        /// <summary>
        /// Find all UserRoles
        /// </summary>
        /// <returns>UserRole collection</returns>
        public IQueryable<UserRole> FindAllUserRoles()
        {
            return db.UserRoles;
        }

        /// <summary>
        /// Get Role by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns>String UserRole</returns>
        public string GetRoleById(int id)
        {
            return (from a in db.UserRoles
                    where a.Id == id
                    select a.Role).SingleOrDefault();
        }

        /// <summary>
        /// Get ID by Role
        /// </summary>
        /// <param name="role"></param>
        /// <returns>Int Id</returns>
        public int GetIdByRole(string role)
        {
            return (from a in db.UserRoles
                    where a.Role == role
                    select a.Id).SingleOrDefault();
        }

        #endregion
    }
}
